#include "sn_uart.h"
#include "sn_atc.h"
#include <string.h>
#include "ws2812b.h"

#define ESP_UART_NAME "uart2" /* 串口设备名称 */
struct uart_para esp_upara = {0};

static const struct atc_set_data esp_cmd[] =
	{
		{"AT", "OK", 3, 10, RT_TRUE},
		{"AT+CWMODE=1", "OK", 3, 10, RT_TRUE},
		//{"AT+CWJAP=\"USR-G800V2-2370\",\"23702370\"", "WIFI GOT IP", 3, 10, RT_TRUE},
		{"AT+CWJAP=\"m16\",\"13188818820\"", "WIFI GOT IP", 3, 10, RT_TRUE},
		{"AT+CIPSTART=\"TCP\",\"192.168.43.173\",3636", "CONNECT", 3, 10, RT_TRUE},
		{"AT+CIPMODE=1", "OK", 3, 10, RT_TRUE},
		{"AT+CIPSEND", ">", 3, 10, RT_TRUE},
};

void esp_flush(rt_device_t dev)
{
	struct uart_recv_msg msg = {0};

	while ((rt_mq_recv(esp_upara.mq, &msg, sizeof(msg), 20)) == RT_EOK)
	{
		rt_kprintf("flush rdbuf:%s\n", msg.buff);
		// rt_thread_mdelay(20);
	}
}
rt_size_t esp_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size, int timeout)
{
	struct uart_recv_msg msg = {0};
	if (rt_mq_recv(esp_upara.mq, &msg, sizeof(msg), timeout) == RT_EOK)
	{
		rt_memcpy(buffer, msg.buff, msg.size);
		return msg.size;
	}
	else
	{
		return 0;
	}
}
rt_size_t esp_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size, int timeout)
{
	return rt_device_write(dev, 0, buffer, size);
}

static struct atc_set_ops esp_ops =
	{
		esp_flush,
		esp_read,
		esp_write,
};


int esp_process()
{
	esp_upara.name = ESP_UART_NAME;

	/* 查找系统中的串口设备 */
	rt_device_t serial = rt_device_find(esp_upara.name);
	if (!serial)
	{
		rt_kprintf("find %s failed!\n", esp_upara.name);
		return RT_ERROR;
	}
	rt_kprintf("find %s success!\n", esp_upara.name);

	/* 以中断接收及轮询发送模式打开串口设备 */
	int result = rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);

	rt_kprintf("open %s :%d!\n", esp_upara.name, result);

	char name[32] = {0};
	rt_sprintf(name, "%s_mq", esp_upara.name);
	esp_upara.mq = rt_mq_create(name, sizeof(struct uart_recv_msg), 3, RT_IPC_FLAG_PRIO);

	rt_thread_t uart_service_thread = rt_thread_create("esp_uart_service", uart_service_thread_entry, &esp_upara, 4096, 15, 20);
	if (uart_service_thread == RT_NULL)
	{
		rt_kprintf("create uart_service_thread failed.");
		return RT_ERROR;
	}
	else
	{
		rt_kprintf("create uart_service_thread success.");
		rt_thread_startup(uart_service_thread);
	}

	rt_thread_mdelay(100);

	result = atc_set(&esp_ops, serial, esp_cmd, sizeof(esp_cmd) / sizeof(esp_cmd[0]));
	rt_kprintf("atc_set %d!\n", result);

	esp_ops.write(serial, 0, "online\r\n", strlen("online\r\n"), 0);

	int state = 0;
	int sn = 0;
	char sl[1024] = {0};
	int vol = 0;
	char buff[128] = {0};
	while (1)
	{
		struct uart_recv_msg msg = {0};
		if (rt_mq_recv(esp_upara.mq, &msg, sizeof(msg), 1000) == RT_EOK)
		{
			if (msg.size < 1)
			{
				continue;
			}
			else
			{
			    if(1)
				{
					if ((msg.buff[0] >= 0x30) && (msg.buff[0] <= 0x3F))
					{
						rt_kprintf("msg.buff: %s\n", msg.buff);
						wifi_led_contrl(msg.buff);
					}
					else
					{
						continue;
					}
				}
				else
				{
					continue;
				}
			}
		}
		else
		{
		}
	}
	return RT_EOK;
}
MSH_CMD_EXPORT(esp_process, esp_process)

int wifi_service_init()
{
    rt_thread_t wifiservice_thread = rt_thread_create("espwifi", esp_process, RT_NULL, 4096, 14, 20);
    if (wifiservice_thread != RT_NULL)
    {
        rt_thread_startup(wifiservice_thread);
    }
    else
    {
        rt_kprintf("wifiservice_thread create failed.\n");
        return RT_ERROR;
    }
}
